#!/usr/bin/python
# -*- coding: utf-8 -*-

#-------------------------------------------------
#Create At: 2008-5-30 上午01:43:46 
#Create By: Fla.sam
#
#DESC: SNote Windows Client
#
#-------------------------------------------------

import os
import sconf
import wx
#import images

_ = wx.GetTranslation

def opj( path ):
    """Convert paths to the platform-specific separator"""
    st = apply( os.path.join, tuple( path.split( '/' ) ) )
    # HACK: on Linux, a leading / gets lost...
    if path.startswith( '/' ):
        st = '/' + st
    return st

def AboutSNote():
    info = wx.AboutDialogInfo()
    info.Name = sconf.__NAME__
    info.Version = sconf.__VERSION__
    info.Copyright = sconf.__COPYRIGHT__
    info.Description = sconf.__DESCRIPTION__
    info.WebSite = ( sconf.__WEBSITE__, _( "SNote's home page" ) )
    info.Developers = ['fla.sam']
    info.License = sconf.__LICENSE__

    # Then we call wx.AboutBox giving it that info object
    wx.AboutBox( info )

class SnoteTaskBarIcon( wx.TaskBarIcon ):
    TBMENU_SHOW_ALL_NOTE = wx.NewId()
    TBMENU_HIDE_ALL_NOTE = wx.NewId()
    TBMENU_NEW_NOTE  = wx.NewId()
    TBMENU_ABOUT_SNOTE  = wx.NewId()
    TBMENU_EXIT_SNOTE   = wx.NewId()
    TBMENU_SNOTE_OPTIONS = wx.NewId()
    
    def __init__( self, frame ):
        wx.TaskBarIcon.__init__( self )
        self.frame = frame

        #Set the image
        icon = wx.Icon( opj( 'resources/snote.ico' ), wx.BITMAP_TYPE_ICO )
        self.SetIcon( icon, sconf.__NAME__ )
        self.imgidx = 1
        
        #bind some events
        self.Bind( wx.EVT_TASKBAR_LEFT_DCLICK, self.OnShowAllNote )
        
        #bind SNote Exit
        self.Bind( wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_EXIT_SNOTE )
        
        #bind new note
        self.Bind( wx.EVT_MENU, self.OnNewNote, id=self.TBMENU_NEW_NOTE )
        
        #bind show all Note
        self.Bind( wx.EVT_MENU, self.OnShowAllNote, id=self.TBMENU_SHOW_ALL_NOTE )
        
        #bind Hide all note
        self.Bind( wx.EVT_MENU, self.OnHideAllNote, id=self.TBMENU_HIDE_ALL_NOTE )
        
        #bind SNote about
        self.Bind( wx.EVT_MENU, self.OnAboutSNote, id=self.TBMENU_ABOUT_SNOTE )
        
    def CreatePopupMenu( self ):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.  Just create
        the menu how you want it and return it from this function,
        the base class takes care of the rest.
        """
        menu = wx.Menu()
        menu.Append( self.TBMENU_SHOW_ALL_NOTE, _( 'Show All Note' ) )
        menu.Append( self.TBMENU_HIDE_ALL_NOTE, _( 'Hide All Note' ) )
        menu.Append( self.TBMENU_NEW_NOTE, _( 'New Note' ) )
        menu.AppendSeparator()
        #menu.Append(self.TBMENU_CHANGE_SNOTE_MODE, _('Change Snote Mode'))
        menu.Append( self.TBMENU_SNOTE_OPTIONS, _( 'Options' ) )
        menu.AppendSeparator()
        menu.Append( self.TBMENU_ABOUT_SNOTE, _( 'About' ) )
        menu.Append( self.TBMENU_EXIT_SNOTE, _( 'Exit Snote' ) )
        return menu

    def OnNewNote( self, evt ):
        print 'New Note'

    def OnShowAllNote( self, evt ):
        print 'Show all note'
        
    def OnHideAllNote( self, evt ):
        print 'Hide All Note'
        
    def OnAboutSNote( self, evt ):
        AboutSNote()
        
    def OnTaskBarClose( self, evt ):
        self.Destroy()
        wx.CallAfter( self.frame.ExitMainLoop )

class SNoteSplashScreen( wx.SplashScreen ):
    def __init__( self ):
        bmp = wx.Image( opj( "resources/splash.png" ) ).ConvertToBitmap()
        
        wx.SplashScreen.__init__( self, bmp,
                                 wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
                                 5000, None, -1 )
        self.Bind( wx.EVT_CLOSE, self.OnClose )
        self.fc = wx.FutureCall( 2000, self.ShowMain )

    def OnClose( self, evt ):
        # Make sure the default handler runs too so this window gets
        # destroyed
        evt.Skip()
        self.Hide()
        
        # if the timer is still running then go ahead and show the
        # main frame now
        if self.fc.IsRunning():
            self.fc.Stop()
            self.ShowMain()

    def ShowMain( self ):
        if self.fc.IsRunning():
            self.Raise()
        
class SNoteApp( wx.App ):
    '''
    SNote main app class
    '''
    def OnInit( self ):
        splash = SNoteSplashScreen()
        splash.Show()
        
        self.tbicon = SnoteTaskBarIcon( self )
        return True

def main():
    app = SNoteApp( False )
    app.MainLoop()

if __name__ == '__main__':
    main()